"""
Case Type   : USTORE
Case Name   : 修改调优参数enable_indexscan
Create At   : 2025/4/1
Owner       : wan005
Description :
    1.创建数据库
    2.查询该数据库参数enable_indexscan默认值
    3.修改enable_indexscan值
    4.查看参数值
    5.重置参数
    6.查看参数值
Expect      :
    1.恢复参数默认值
    2.默认值为on
    3.参数修改成功
    4.参数值为off
    5.重置成功
    6.参数值为on
History     :
"""
import os
import unittest
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.user_node = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.com_db = 'db_object_198'

    def test_ustore(self):
        text = '----step1:创建数据库;expect:成功----'
        self.logger.info(text)
        sql_cmd1 = f'create database {self.com_db};'
        res1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.logger.info(res1)
        self.assertEqual('CREATE DATABASE', res1, text + '执行失败')

        text = '-----step2:查询参数默认值;expect:成功-----'
        self.logger.info(text)
        sql_cmd2 = 'show enable_indexscan;'
        res2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.com_db)
        self.common.equal_sql_mdg(res2, 'enable_indexscan',
                                  'on', '(1 row)', flag=1)

        text = '----step3:修改参数enable_indexscan值off;expect:成功----'
        self.logger.info(text)
        sql_cmd3 = f'alter database {self.com_db} set enable_indexscan=off;'
        res3 = self.sh_primy.execut_db_sql(sql_cmd3)
        self.logger.info(res3)
        self.assertEqual(self.constant.ALTER_DATABASE_SUCCESS_MSG, res3,
                         text + '执行失败')

        text = '----step4:查看参数enable_indexscan值;expect:成功----'
        self.logger.info(text)
        sql_cmd4 = 'show enable_indexscan;'
        res4 = self.sh_primy.execut_db_sql(sql_cmd4, dbname=self.com_db)
        self.logger.info(res4)
        self.common.equal_sql_mdg(res4, 'enable_indexscan', 'off', '(1 row)',
                                  flag=1)

        text = '----step5:重置参数enable_indexscan值;expect:成功----'
        self.logger.info(text)
        sql_cmd5 = f'alter database {self.com_db} reset enable_indexscan;'
        res5 = self.sh_primy.execut_db_sql(sql_cmd5, dbname=self.com_db)
        self.logger.info(res5)
        self.assertEqual(self.constant.ALTER_DATABASE_SUCCESS_MSG, res5,
                         text + '执行失败')

        text = '----step6:查看参数enable_indexscan值;expect:成功----'
        self.logger.info(text)
        sql_cmd6 = 'show enable_indexscan;'
        res6 = self.sh_primy.execut_db_sql(sql_cmd6, dbname=self.com_db)
        self.logger.info(res6)
        self.common.equal_sql_mdg(res6, 'enable_indexscan', 'on', '(1 row)',
                                  flag=1)

    def tearDown(self):
        text = '-----step1:清理环境；expect:成功-----'
        self.logger.info(text)
        sql_cmd1 = f'drop database {self.com_db};'
        res1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.logger.info(res1)
        self.assertEqual('DROP DATABASE', res1, text + '执行失败')
        self.logger.info(f'---{os.path.basename(__file__)} end---')
